<?php

namespace app\admin\model;

use think\Model;
use think\Cache;

use util\Tree;

class AuthRule extends Model
{

  protected $table = 'a_auth_rule';
  protected $pk    = 'id';
  protected $autoWriteTimestamp = true;

  private $userId ;
  private $isAdmin ;

  //取得当前UID的权限规则，并组合数据
  public function getAuthRuleData($userId,$isAdmin = 0){
    $this->userId  = $userId;
    $this->isAdmin  = $isAdmin;
    return Cache::tag('admin_cache_data')->remember('adminUser_cache_data_'.$this->userId,function(){
      $sidebar       = [];
      $RuleData      = [];
      $result        = [];

      $db = new AdminUsers();
      $authGroupData = $db->getUserGroupRule($this->userId);


      if(isset($authGroupData['rules'])){
        if($this->isAdmin){
          $authRuleData  = $this->field('id,name,menu_name,pid,sort,type,icon,is_show')
                                ->where('status',1)
                                ->order('sort desc')
                                ->select();
        }else {
          $authRuleData  = $this->field('id,name,menu_name,pid,sort,type,icon,is_show')
                                ->where('id','in',$authGroupData['rules'])
                                ->where('status',1)
                                ->order('sort desc')
                                ->select();
        }


         //精简数据
        foreach ($authRuleData as $k => $v) {
           $dump['id']        = $v['id'];
           $dump['name']      = $v['name'];
           $dump['menu_name'] = $v['menu_name'];
           $dump['icon']      = $v['icon'];
           $dump['is_show']   = $v['is_show'];
           $dump['pid']       = $v['pid'];
           $RuleData[]        = $dump;
           unset($dump);
        }

        // 将菜单转成Tree的形式并去除无子菜单的项
        foreach (Tree::toLayer($RuleData) as $key => $value) {
          if (array_key_exists('child',$value)) {
            $result[] = $value;
          }
        }

        unset($authRuleData);
        unset($RuleData);
        unset($sidebar);

      }

      unset($db);
      unset($authGroupData);

      return  $result;
     });
  }

   //取得所有的规则，并组合数据
  public function getAll(){
    return Cache::tag('admin_cache_data')->remember('adminUser_cache_data_all_auth',function(){
      $authRuleData  = $this->field('id,name,menu_name,pid,title,sort,condition,type,icon,is_show,status,can_delete')
                            ->order('sort','desc')
                            ->select();
      //精简数据
     foreach ($authRuleData as $k => $v) {
        $dump['id']        = $v['id'];
        $dump['pid']       = $v['pid'];
        $dump['name']      = $v['name'];
        $dump['menu_name'] = $v['menu_name'];
        $dump['title']     = $v['title'];
        $dump['condition'] = $v['condition'];
        $dump['sort']      = $v['sort'];
        $dump['icon']      = $v['icon'];
        $dump['is_show']   = $v['is_show'];
        $dump['can_delete']= $v['can_delete'];
        $dump['status']    = $v['status'];
        $RuleData[]        = $dump;
        unset($dump);
     }
     // 将菜单转成Tree的形式并返回
     return  Tree::toLayer($RuleData);
    });

  }

  public function getRule($id){
    return $this->field('id,name,menu_name,pid,title,sort,condition,type,icon,is_show,status')->find($id);
  }

  //新增节点规则
  public function addRule($data){
    Cache::rm('adminUser_cache_data_'.session('admin_user_id'));
    Cache::rm('adminUser_cache_data_all_auth');

    return $this->data($data)->allowField(true)->save();
  }

  public function updateRule($data){

    Cache::rm('adminUser_cache_data_'.session('admin_user_id'));
    Cache::rm('adminUser_cache_data_all_auth');

    return $this->update($data);
  }

  public function deleteRule($id){

    Cache::rm('adminUser_cache_data_'.session('admin_user_id'));
    Cache::rm('adminUser_cache_data_all_auth');

    if (is_array($id)) {
      return $this->destroy($id);
    }
    return $this->destroy($id);
  }

}
